/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.manager;

import com.google.inject.Inject;
import com.wideplay.warp.persist.Transactional;
import exatrez.dao.ShipVisitCounterDAO;
import exatrez.dao.ShipVisitDAO;
import exatrez.dao.ShipVisitFileDAO;
import exatrez.model.ShipVisit;
import exatrez.model.ShipVisitCounter;
import exatrez.model.ShipVisitFile;
import exatrez.model.User;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Mansur
 */
public class VisitManager {
    
    @Inject
    private ShipVisitDAO shipVisitDAO;
    
    @Inject
    private ShipVisitFileDAO shipVisitFileDAO;
    
    @Inject
    private ShipVisitCounterDAO shipVisitCounterDAO;
    
    private static String SUCCESS = "success";
    private static String FAIL = "fail";
    
    @Inject
    protected com.google.inject.Provider<Session> sessionProvider;
    
    private static Logger LOG = LoggerFactory.getLogger(VisitManager.class);
    
    @Transactional
    public String saveOrUpdateVisit(ShipVisit shipVisit, ShipVisitCounter visitCounter){
        String result = SUCCESS;        
        try{
            shipVisitDAO.saveOrUpdate(shipVisit);
        }catch(Exception ex){
            LOG.error(ex.getMessage());
//            ex.printStackTrace();//for development use only
            result = FAIL;
        }        
        return result;
    }
    
    @Transactional
    public String deleteVisit(ShipVisit shipVisit, ShipVisitCounter visitCounter){
        String result = SUCCESS;
        LOG.info("shipVisit.id :"+shipVisit.getShipVisitId());
        LOG.info("shipVisitCounter.id :"+visitCounter.getSvCounterId());
        try{
            shipVisit.setDeleted("Y");
            shipVisitDAO.saveOrUpdate(shipVisit);            
            shipVisitCounterDAO.saveOrUpdate(visitCounter);
        }catch(Exception ex){
            LOG.error(ex.getMessage());
            ex.printStackTrace();//for development use only
            result = FAIL;
        }
        return result;
    }
    
    @Transactional
    public String saveorUpdateVisitFile(ShipVisitFile visitFile){
        String result = SUCCESS;
        try{
            shipVisitFileDAO.saveOrUpdate(visitFile);
        }catch(Exception ex){
            LOG.error(ex.getMessage());
            ex.printStackTrace(); //for development use only
            result = FAIL;
        }
        return result;
    }
    
    public List<ShipVisitFile> getListFile(Integer visitId){
        StringBuilder sb = new StringBuilder("select s from ShipVisitFile s ")
                .append("where s.deleted = 'N' and s.shipVisit.shipVisitId = :visitId");
        
        Session session = sessionProvider.get();
        
        Query query = session.createQuery(sb.toString()).setParameter("visitId", visitId);
        
        return query.list();
    }
    
    @Transactional
    public String deleteFail(ShipVisitFile visitFail){
        String result = SUCCESS;
        LOG.info("shipVisitFile.id :"+visitFail.getSvFileId());
        try{
            visitFail.setDeleted("Y");
            shipVisitFileDAO.saveOrUpdate(visitFail);
        }catch(Exception ex){
            LOG.error(ex.getMessage());
            ex.printStackTrace();//for development use only
            result = FAIL;
        }
        return result;
    }
    
    @Transactional
    public String saveOrUpdateVisitCounter(ShipVisitCounter visitCounter){
        String result = SUCCESS;
        LOG.info("ShipVisitCounter.Id :"+visitCounter.getSvCounterId());
        try{
            shipVisitCounterDAO.saveOrUpdate(visitCounter);
        }catch(Exception ex){
            LOG.error(ex.getMessage());
            ex.printStackTrace();//for development use only
            result = FAIL;
        }
        return result;
    }
    
    public List<ShipVisitCounter> getListCounter(User user, String year, String departmentId){
        StringBuilder sb = new StringBuilder("select s from ShipVisitCounter s, User u ")
                .append("where s.user = u ");
        
        Session session = sessionProvider.get();
        
        if(user != null){
            sb.append("and s.user.userId = :userId ");
        }
        if(year != null){
            sb.append("and s.year = :year ");
        }
        if(departmentId != null){
            sb.append("and u.department.departmentId = :departmentId ");
        }
        sb.append("order by s.infoAudit.createDate");
        
        Query query = session.createQuery(sb.toString());
        if(user != null){
            query.setParameter("userId", user.getUserId());
        }
        if(year != null){
            query.setParameter("year", year);
        }
        if(departmentId != null){
            query.setParameter("departmentId", Integer.parseInt(departmentId));
        }
        return query.list();
    }
    
    public List<ShipVisit> getListVisited(User user){
        StringBuilder sb = new StringBuilder("select s from ShipVisit s ")
                .append("where user.userId  = :userId and deleted = 'N' ");
        
        Session session = sessionProvider.get();
        
        Query query = session.createQuery(sb.toString()).setParameter("userId", user.getUserId());
        
        return query.list();
    }
    
    public List<ShipVisit> getListPastVisitStaff(User user, String pastYear){
        StringBuilder sb = new StringBuilder("select s from ShipVisit s ")
                .append("where s.user.userId  = :userId and s.deleted = 'N' ")
                .append("and YEAR(s.infoAudit.createDate) = :year");
        
        Session session = sessionProvider.get();
        Query query = session.createQuery(sb.toString());
        query.setParameter("userId", user.getUserId());
        query.setParameter("year", Integer.parseInt(pastYear));
        
        return query.list();
    }
    
    public List<ShipVisitCounter> getListPastVisitCounterStaff(User user, String pastYear){
        StringBuilder sb = new StringBuilder("select s from ShipVisitCounter s ")
                .append("where s.user.userId  = :userId ")
                .append("and s.year = :year");
        
        Session session = sessionProvider.get();
        Query query = session.createQuery(sb.toString());
        query.setParameter("userId", user.getUserId());
        query.setParameter("year", pastYear);
        
        return query.list();
    }
}
